<template>
    <div class="metidea">
        <el-upload
            class="avatar-uploader"
            action="https://upload-z1.qiniup.com"
            :data="uploadImg"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-button @click="change">点击修改头像</el-button>
    </div>
</template>
<script>
import axios from "axios"
    export default {
        data() {
        return {
            imageUrl: '',
            uploadImg:{
                token:""
            }
        };
        },
        created(){
            this.$api.$get("/uploadImg").then((res)=>{
			this.uploadImg.token = res.data.token
		})
        },
        methods: {
        //上传照片
        handleAvatarSuccess(res, file) {
            this.imageUrl = "http://q6k6s03kn.bkt.clouddn.com/"+file.response.key
        },
        beforeAvatarUpload(file) {
            console.log(file)   
        },
        change(){
            var session = JSON.parse(window.sessionStorage.getItem("userInfo"))
            this.$api.$post("/upDataUserImg",{id:session._id,userImg:this.imageUrl}).then((res)=>{
                console.log(res)
                if(res.data.code == 200){
                    this.$message(res.data.msg);  
                }
            })
        } 
        }
    }
</script>
<style>
    .metidea{
        padding:8px;
    }
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>